<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: product_filter.php 135 2013-09-22 07:30:55Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 135 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-22 07:30:55 +0000 (Sun, 22 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

if((isset($_GET['filter']) && !empty($_GET['filter'])) OR isset($_GET['minp'])) {
	$minp = $_GET['minp'];
	$maxp = $_GET['maxp'];
	$filter_products = '';
	$pd = '';
	
	$filter_param = '?minp='.$minp.'&maxp='.$maxp;

	if(isset($_GET['filter']) && !empty($_GET['filter'])) {
		foreach($_GET['filter'] AS $name => $item)
			$filter_param .= '&filter[]='.$item;
		
		if(isset($_GET['sub_filter'])) {		
			foreach($_GET['sub_filter'] AS $sub_name => $sub_item) {
				$filter_param .= '&sub_filter[]='.$sub_item;
				$session_sub_filter[] = $sub_item;
			}
			$in_sub_filters .= '\''.implode('\',\'', $_GET['sub_filter']).'\'';
			$filters .= (strlen(implode(',', $_GET['sub_filter']))-1);
			
			$product_ids = $db->db_query("SELECT 
											GROUP_CONCAT(filter_id) AS filter,
												products_id
											FROM
												".TABLE_PRODUCTS_TO_FILTERS."
											WHERE
												filter_id IN(".$in_sub_filters.")
											GROUP BY
												products_id
											HAVING
												CHAR_LENGTH(filter) > ".$filters);
			
			if($product_ids->_numOfRows) {
				while(!$product_ids->EOF) {	
					$p[] = $product_ids->fields['products_id'];
					$product_ids->MoveNext();
				}
			}
			
			$pd = create_in_product($p);
			
			$getNames = $db->db_query("SELECT 
											filter_name
										FROM
											".TABLE_PRODUCTS_FILTERS."
										WHERE 
											filter_id IN (".$in_sub_filters.")");
	
			while(!$getNames->EOF) {
				$filter_name[] = $getNames->fields['filter_name'];
				$getNames->MoveNext();
			}
			
			$filter_names = implode(', ', $filter_name);
	
		}
		
		$brand = '';
	}
	
	if($_SESSION['customers_status']['customers_fsk18_display'] == '0')
		$fsk_lock = ' AND p.products_fsk18 != 1';
	
	if(GROUP_CHECK == 'true')
		$group_check = " AND p.group_permission_".$_SESSION['customers_status']['customers_status_id']." = 1 ";
		
	$check = $db->db_query("SELECT specials_id FROM ".TABLE_SPECIALS);
	
	if(defined('MODULE_SMALL_BUSINESS_STATUS') && MODULE_SMALL_BUSINESS_STATUS == 'true') {
		if($check->_numOfRows) {
			$filter_sql = "SELECT DISTINCT
								p.products_id,
								p.products_quantity,
								p.products_fsk18,
								pd.products_name,
								pd.products_short_description,
								p.products_image,
								p.products_price,
								p.products_vpe,
								p.products_vpe_status,
								p.products_vpe_value
							FROM
								".TABLE_SPECIALS." s,
								".TABLE_PRODUCTS." p					
								LEFT JOIN ".TABLE_PRODUCTS_DESCRIPTION." AS pd 
									ON (p.products_id = pd.products_id AND pd.language_id = '".(int)$_SESSION['languages_id']."')
							WHERE 
								p.products_status = '1'
							AND
								(IF(s.specials_price_".$_SESSION['customers_status']['customers_status_id']." = p.products_price, s.specials_price_".$_SESSION['customers_status']['customers_status_id']." >= '".($minp-0.01)."', p.products_price >= '".($minp-0.01)."')
								AND 					 
									(p.products_price <= '".($maxp+0.01)."'))
							".$pd.$group_check.$fsk_lock;
	
		} else {	
			$filter_sql = "SELECT
			                        p.products_id, 
			                        p.products_quantity, 
			                        p.products_fsk18, 
			                        pd.products_name, 
			                        pd.products_short_description, 
			                        p.products_image, 
			                        p.products_price, 
			                        p.products_vpe, 
			                        p.products_vpe_status, 
			                        p.products_vpe_value, 
			                        p.products_tax_class_id 
			                   FROM 
			                        ".TABLE_PRODUCTS." p 
			                        LEFT JOIN ".TABLE_PRODUCTS_DESCRIPTION." AS pd  
			                             ON (p.products_id = pd.products_id AND pd.language_id = '".(int)$_SESSION['languages_id']."')  
			                        ".$field." 
			                   WHERE  
			                        (p.products_price >= '".($minp-0.01)."' AND p.products_price <= '".($maxp+0.01)."') 
			                        ".$pd." 
			                   AND 
			                        p.products_status = '1' 
			                        ".$group_check.$fsk_lock;
		}
		
	} else {
		if($check->_numOfRows) {
			$filter_sql = "SELECT DISTINCT
								p.products_id,
								p.products_quantity,
								p.products_fsk18,
								pd.products_name,
								pd.products_short_description,
								p.products_image,
								p.products_price,
								p.products_vpe,
								p.products_vpe_status,
								p.products_vpe_value,
								p.products_tax_class_id
							FROM
								".TABLE_SPECIALS." s,
								".TABLE_PRODUCTS." p					
								LEFT JOIN ".TABLE_PRODUCTS_DESCRIPTION." AS pd 
									ON (p.products_id = pd.products_id AND pd.language_id = '".(int)$_SESSION['languages_id']."')
								INNER JOIN
									(SELECT t.tax_rates_id, t.tax_rate AS tax FROM ".TABLE_TAX_RATES." t) AS x
									ON x.tax_rates_id = p.products_tax_class_id 
							WHERE 
								p.products_status = '1'
							AND
								(IF((s.specials_price_".$_SESSION['customers_status']['customers_status_id']."+s.specials_price_".$_SESSION['customers_status']['customers_status_id']."/100*x.tax) = (p.products_price+p.products_price/100*x.tax),(s.specials_price_".$_SESSION['customers_status']['customers_status_id']."+s.specials_price_".$_SESSION['customers_status']['customers_status_id']."/100*x.tax)>= '".($minp-0.01)."',(p.products_price+p.products_price/100*x.tax) >= '".($minp-0.01)."')
								AND 					 
									((p.products_price+p.products_price/100*x.tax) <= '".($maxp+0.01)."'))
							".$pd.$group_check.$fsk_lock;
	
		} else {	
			$filter_sql = "SELECT
			                        p.products_id, 
			                        p.products_quantity, 
			                        p.products_fsk18, 
			                        pd.products_name, 
			                        pd.products_short_description, 
			                        p.products_image, 
			                        p.products_price, 
			                        p.products_vpe, 
			                        p.products_vpe_status, 
			                        p.products_vpe_value, 
			                        p.products_tax_class_id 
			                   FROM 
			                        ".TABLE_PRODUCTS." p 
			                        LEFT JOIN ".TABLE_PRODUCTS_DESCRIPTION." AS pd  
			                             ON (p.products_id = pd.products_id AND pd.language_id = '".(int)$_SESSION['languages_id']."') 
			                        INNER JOIN 
			                             (SELECT t.tax_rates_id, t.tax_rate AS tax FROM ".TABLE_TAX_RATES." t) AS x 
			                             ON x.tax_rates_id = p.products_tax_class_id  
			                        ".$field." 
			                   WHERE  
			                        ((p.products_price+p.products_price/100*x.tax) >= '".($minp-0.01)."' AND (p.products_price+p.products_price/100*x.tax) <= '".($maxp+0.01)."') 
			                        ".$pd." 
			                   AND 
			                        p.products_status = '1' 
			                        ".$group_check.$fsk_lock;
		}
	}
	if($pIDs = $db->db_query($filter_sql)) {
		while(!$pIDs->EOF) {
			$p_ids[] = $pIDs->fields['products_id'];
			$pIDs->MoveNext();
		}
		$filter_products = create_in_product($p_ids);
		
		if(isset($_GET['filter_id'])) {
			$getBrand = $db->db_query("SELECT brand_name FROM ".TABLE_BRAND." WHERE brand_id = '".$_GET['filter_id']."' ");
			$brands = $getBrand->fields['brand_name'];
		}
		
		$filter_title = 'Suchparameter: '.$price->format($minp, true).' -'.$price->format($maxp, true).(!empty($filter_names) ? ', '.$filter_names : '');
		
		if(!empty($brands) || !empty($sort))
			$filter_title .= '<br />Suchfilter: '.$brands.(empty($brands)?'':', ').$sort;
	}
}